<?php

class ContacterController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_contacter where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select * from crm_contacter where deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $contacter_id = $_REQUEST['contacter_id'];

        $contacter = null;
        if($contacter_id==0){
            //新增
        }else{
            //编辑
            $contacter = $this->connection->createCommand("select * from crm_contacter where contacter_id = :contacter_id")->bindParam(':contacter_id',$contacter_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['contacter'=>$contacter]);
    }

    public function actionSavedata(){

        $contacter_id = $_REQUEST['contacter_id'];
        $contacter_name = $_REQUEST['contacter_name'];
        $position = $_REQUEST['position'];
        $email = $_REQUEST['email'];
        $phone = $_REQUEST['phone'];
        $tel = $_REQUEST['tel'];

        if($contacter_id!=null && $contacter_id!=""){
            //编辑
            $this->connection->createCommand("update crm_contacter set contacter_name = :contacter_name,phone = :phone,
position=:position,email=:email,tel=:tel where contacter_id = :contacter_id")
                ->bindParam(':contacter_id',$contacter_id,PDO::PARAM_INT)
                ->bindParam(':contacter_name',$contacter_name,PDO::PARAM_STR)
                ->bindParam(':position',$position,PDO::PARAM_STR)
                ->bindParam(':email',$email,PDO::PARAM_STR)
                ->bindParam(':phone',$phone,PDO::PARAM_STR)
                ->bindParam(':tel',$tel,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_contacter (contacter_name,position,email,phone,tel) 
values (:contacter_name,:position,:email,:phone,:tel) ")
                ->bindParam(':contacter_name',$contacter_name,PDO::PARAM_STR)
                ->bindParam(':position',$position,PDO::PARAM_STR)
                ->bindParam(':email',$email,PDO::PARAM_STR)
                ->bindParam(':phone',$phone,PDO::PARAM_STR)
                ->bindParam(':tel',$tel,PDO::PARAM_STR)
                ->execute();
        }
    }

    public function actionRemove(){
        $contacter_id = $_REQUEST['contacter_id'];
        $this->connection->createCommand("update crm_contacter set deleted = 1 
where contacter_id = :contacter_id")
            ->bindParam(':contacter_id',$contacter_id,PDO::PARAM_INT)
            ->execute();
    }

    public function actionView(){

        $contacter_id = $_REQUEST['contacter_id'];
        $contacter = $this->connection->createCommand("select * from crm_contacter where contacter_id = :contacter_id")->bindParam(':contacter_id',$contacter_id,PDO::PARAM_INT)->queryRow();

        $this->render('view',['contacter'=>$contacter]);
    }

}